package org.lanqiao.userprofile.movie.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lanqiao.userprofile.movie.common.CommonUtil;
import org.lanqiao.userprofile.movie.common.TextToImg;
import org.lanqiao.userprofile.movie.entity.DwdUserInfo;
import org.lanqiao.userprofile.movie.entity.OdsMovieInfo;
import org.lanqiao.userprofile.movie.service.DwdUserInfoService;
import org.lanqiao.userprofile.movie.service.OdsMovieInfoService;
import org.lanqiao.userprofile.movie.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author root
 * @since 2023-09-25
 */
@Controller
@RequestMapping("/ods-movie-info")
public class OdsMovieInfoController {

    @Autowired
    private DwdUserInfoService dwdUserInfoService;

    @Autowired
    private OdsMovieInfoService odsMovieInfoService;

    @Autowired
    private WebSocketServer webSocketServer;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, @RequestParam(defaultValue = "1") int pageNum) throws IOException, InterruptedException {
        Object name = request.getSession().getAttribute("name");
        if (name == null) {
            return "login.html";
        }
        LambdaQueryWrapper<DwdUserInfo> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(DwdUserInfo::getUserToken, name);
        DwdUserInfo dwdUserInfo = dwdUserInfoService.getOne(userQueryWrapper);
        String tag = CommonUtil.getTag(dwdUserInfo);

        int size = 10; // 每页显示的数量
        System.out.println(pageNum);
        // 调用Service层方法进行分页查询
        Page<OdsMovieInfo> moviePage = odsMovieInfoService.selectPage(pageNum, size, tag);

        request.setAttribute("odsMovieInfoList", moviePage.getRecords());
        request.setAttribute("currentPage", moviePage.getCurrent());
        request.setAttribute("totalPages", moviePage.getPages());
        request.setAttribute("page", moviePage);

        request.setAttribute("sex", dwdUserInfo.getSex());

        request.setAttribute("touxiang", TextToImg.getImg(dwdUserInfo.getSex()));


        // hot movie
        List<OdsMovieInfo> hotOdsMovieInfoList = odsMovieInfoService.selectTop();
        //hotOdsMovieInfoList.forEach(System.out::println);
        request.setAttribute("hotOdsMovieInfoList", hotOdsMovieInfoList);

        request.setAttribute("name", name);

        return "index.html";
    }

    @RequestMapping("/admin")
    public String admin(HttpServletRequest request, @RequestParam(defaultValue = "1") int pageNum) throws IOException {
        int size = 5; // 每页显示的数量
        System.out.println(pageNum);
        // 调用Service层方法进行分页查询
        Page<OdsMovieInfo> moviePage = odsMovieInfoService.selectPage(pageNum, size);

        request.setAttribute("odsMovieInfoList", moviePage.getRecords());
        request.setAttribute("currentPage", moviePage.getCurrent());
        request.setAttribute("totalPages", moviePage.getPages());
        request.setAttribute("page", moviePage);
        //moviePage.getRecords().forEach(System.out::println);
        return "admin.html";
    }

    @RequestMapping("/send")
    @ResponseBody
    public JSONObject send(HttpServletRequest request, @RequestParam long id) {
        JSONObject jsonObject = new JSONObject();
        try {
            LambdaQueryWrapper<OdsMovieInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OdsMovieInfo::getId, id);
            OdsMovieInfo odsMovieInfo = odsMovieInfoService.getOne(queryWrapper);
            String tags = odsMovieInfo.getTags();
            String[] tagList = tags.split(",");
            List<String> sexList = new ArrayList<String>();
            List<String> businessList = new ArrayList<String>();
            for (String tag : tagList) {
                String sex = CommonUtil.getSex(tag);
                if (sex != null) {
                    sexList.add(sex);
                }
                String business = CommonUtil.getBusiness(tag);
                if (business != null) {
                    businessList.add(business);
                }
            }
            Set<String> set = new HashSet<>();
            if (sexList.size() > 0) {

                List<String> list = dwdUserInfoService.listObjs(new LambdaQueryWrapper<DwdUserInfo>().in(DwdUserInfo::getSex, sexList).select(DwdUserInfo::getUserToken), Object::toString);

                if (!list.isEmpty()) {
                    set.addAll(list);
                }
            }
            if (businessList.size() > 0) {
                List<String> list = dwdUserInfoService.listObjs(new LambdaQueryWrapper<DwdUserInfo>().in(DwdUserInfo::getBusiness, businessList).select(DwdUserInfo::getUserToken), Object::toString);
                if (!list.isEmpty()) {
                    set.addAll(list);
                }
            }
            System.out.println(set.size());
            webSocketServer.sendMessageToAll(set, "你感兴趣的电影:" + odsMovieInfo.getTitle() + "上新了");
        } catch (Exception e) {
            jsonObject.put("msg", "fail");
            return jsonObject;
        }
        jsonObject.put("msg", "success");
        return jsonObject;
    }
}
